<?php

namespace App\Http\Controllers\Wechat\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ShopRepo;
use App\Repos\CouponRepo;
use App\Http\Requests\Api\Shop\ShopRequest;
use App\Http\Requests\Api\Shop\CouponRequest;
use EasyWeChat;

class IndexController extends Controller
{
    /**
     * 店铺信息
     */
    public function shop(ShopRequest $request)
    {    	
    	$rs['status'] = false;
        $shop_id = $request->shopUser->shop_id;

        $shop = ShopRepo::find(['id'=>$shop_id]);
        //dd($shop);
        $coupons = CouponRepo::getList(['shop_id'=>$shop_id]);
        //dd($coupons);
        if($shop) {       
            $shop->envelope_money = 100;
            $shop->coupons = $coupons;
            $rs['status'] = true;
            $rs['data'] = $shop;
            
             
        }
        
    	return response()->json($rs);
    }

    /**
     * 老板端获取openid
     */
    public function getOpenid(Request $request)
    {
        $rs['status'] = false;
        $result = $data = [];
        $code = trim($request->code);

        $mini = EasyWeChat::miniProgram('shop'); // 小程序
        $result = $mini->auth->session($code); // $code 为wx.login里的code
        if(!array_key_exists('openid', $result)) {
            $rs['msg'] = $result['errmsg'];
            return response()->json($rs);
        }
        
        
               
       
        $data['openid'] = $result['openid'];

        // 激活此账号       
        $where['phone'] = $request->phone;
        $where['code']  = $request->license;
        $flag = ShopRepo::active($where,$data);
        if($flag['status'] == true) {
            $rs['data']['openid']= $result['openid'];
            $rs['data']['token'] = $flag['token'];
            $rs['status'] = true;
            return response()->json($rs);
        }
        $rs['msg'] = $flag['msg'];        
        return response()->json($rs);
    }

    /**
     * 更新店铺信息，商家更新信息需要后台审核，后台审核之前还是显示原来的信息
     */
    public function updateShop(ShopRequest $request)
    {
        $rs['status'] = false;
        $where['id']          = $request->shopUser->shop_id;
        $data['status']      = '-2';
        $data['update_info']  = json_encode($request->pure());

        // if($request->logo) {
        //     $data['logo'] = ShopRepo::saveLogo($request->logo);
        // }
        $flag = ShopRepo::update($where,$data);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];
            return response()->json($rs);
        }
        $rs['status'] = true;
        return response()->json($rs);
    }

    public function addCoupon(CouponRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        $data['shop_id'] = $request->shopUser->shop_id;
        $data['name']    = '满'.$request->min_amount.'减'.$request->value;
        $data['type']       = array_get($data,'type',1); 
        $data['valid_days'] = array_get($data,'valid_days',3);      
        $data['code']    = orderCode('C',$data['shop_id']);
        $flag = CouponRepo::save($data);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];

            return response()->json($rs);
        }
        $rs['status'] = true;
        $rs['data'] = $flag['data'];
        $rs['msg']  = '添加成功';
        return response()->json($rs);
    }

    /**
     * 删除优惠券
     */
    public function delCoupon(CouponRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        $data['shop_id'] = $request->shopUser->shop_id;
        $coupon_id = $request->coupon_id;
        $flag = CouponRepo::delete($coupon_id);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];
            return response()->json($rs);
        }
        $rs['status'] = true;
        $rs['msg']  = '操作成功';
        return response()->json($rs);
    }
}
